package com.concurrency.future;


import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // Future和Runnable的实现类，其中重要的方法run()，get()
        // CAS修改状态state
        // get()时，LockSupport.park 阻塞等待，执行完成时，LockSupport.unpark 唤醒
        FutureTask task = new FutureTask(() -> {
            int a = new Random().nextInt();
            System.out.println(Thread.currentThread().getName() + ", " + a);
            return a;
        });
        new Thread(task).start();

        // Future可以拿到返回的结果
        System.out.println(Thread.currentThread().getName() + ", " + task.get());
    }

}
